package com.example.doctorstation.POJOController;

import com.example.doctorstation.Config.MyResponse;
import com.example.doctorstation.Helper.StringHelper;
import com.example.doctorstation.POJOData.*;
import com.example.doctorstation.POJOMapper.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class PatientDiagnoseController {

    @Resource
    PatientDiagnoseMapper patientDiagnoseMapper;

    @Resource
    private UserMapper userMapper;

    @GetMapping(value="/patientdiagnose")
    public MyResponse getPatientDiagnose(PatientDiagnose diagnose) {
        try {
            // System.out.println(diagnose.getINHOSPITALINFOID() + "  " + diagnose.getPATIENTID());
            return new MyResponse(patientDiagnoseMapper.getPatientDiagnose(diagnose.getPATIENTID()), "已获取该病人的医嘱", "该病人没有医嘱");
        } catch (Throwable e) {
            e.printStackTrace();
            return MyResponse.error(e.getMessage());
        }
    }

    @PostMapping(value="/patientdiagnose")
    public MyResponse addPatientDiagnose(@RequestBody PatientDiagnose diagnose)
    {
        try {
            // 获取当前下医嘱的医生用户信息
            User user = (User) SecurityUtils.getSubject().getPrincipal();
            if (!StringUtils.isEmpty(user)) {
                diagnose.setPDIAGNOSE_DOCTOR_ID(user.getUserID());
                diagnose.setPDIAGNOSE_DOCTOR_NAME(userMapper.findDoctorNameByUserId(user.getUserID()));
            }
            // System.out.println(diagnose);
            return new MyResponse(patientDiagnoseMapper.addPatientDiagnose(diagnose), "已增加该病人的医嘱！", "增加医嘱失败");
        } catch (Throwable e) {
            e.printStackTrace();
            return MyResponse.error(e.getMessage());
        }
    }

    @PutMapping(value="/patientdiagnose")
    public MyResponse updatePatientAdviceStatus(@RequestBody PatientDiagnose diagnose)
    {
        try {
            // System.out.println(diagnose.getPDIAGNOSE_ID());
            return new MyResponse(patientDiagnoseMapper.updatePatientDiagnoseStatus(diagnose.getPDIAGNOSE_ID()), "已执行医嘱", "执行医嘱失败");
        } catch (Throwable e) {
            e.printStackTrace();
            return MyResponse.error(e.getMessage());
        }
    }
}
